﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_account : System.Web.UI.Page
{
    myDataSetTableAdapters.AccountsTableAdapter accAdapter = new myDataSetTableAdapters.AccountsTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            display();
        }
    }

    protected void display()
    {
        myDataSet.AccountsDataTable accTable = accAdapter.GetDataConvert();
        GridView1.DataSource = accTable;
        GridView1.DataBind();
    }

    protected void clear()
    {
        txtId.Text = "";
        txtUser.Text = "";
        txtPass.Text = "";
        txtPass2.Text = "";
        txtFullname.Text = "";
        txtRollNo.Text = "";
        RadDatePicker1.SelectedDate = null;
        RadioButtonList1.SelectedIndex = -1;
        DropDownList1.SelectedValue = "-1";
        lblResult.Text = null;
        lblUserError.Visible = false;
        lblUserDuplicateError.Visible = false;
        lblPassError.Visible = false;
        lblPass2Error.Visible = false;
        lblConfirmPassError.Visible = false;
        lblFullnameError.Visible = false;
        lblRollNoError.Visible = false;
        lblStatusError.Visible = false;
        lblUserTypeError.Visible = false;
        lblDateError.Visible = false;
    }

    protected bool checkEmpty()
    {
        bool status = true;
        if (cmdSearch.Text.ToString() == "Add new")
        {
            if (txtUser.Text.Trim() == "")
            {
                lblUserError.Visible = true;
                status = false;
            }
            else
            {
                lblUserError.Visible = false;
            }
            if (txtPass.Text.Trim() == "")
            {
                lblPassError.Visible = true;
                status = false;
            }
            else
            {
                lblPassError.Visible = false;
            }
            if (txtPass2.Text.Trim() == "")
            {
                lblPass2Error.Visible = true;
                status = false;
            }
            else
            {
                lblPass2Error.Visible = false;
                if (txtPass.Text.Trim() != txtPass2.Text.Trim())
                {
                    lblConfirmPassError.Visible = true;
                    status = false;
                }
                else
                {
                    lblConfirmPassError.Visible = false;
                }
            }
        }
        if (txtFullname.Text.Trim() == "")
        {
            lblFullnameError.Visible = true;
            status = false;
        }
        else
        {
            lblFullnameError.Visible = false;
        }
        if (txtRollNo.Text.Trim() == "")
        {
            lblRollNoError.Visible = true;
            status = false;
        }
        else
        {
            lblRollNoError.Visible = false;
        }
        if (RadDatePicker1.SelectedDate == null)
        {
            lblDateError.Visible = true;
            status = false;
        }
        else
        {
            lblDateError.Visible = false;
        }
        if (RadioButtonList1.SelectedIndex == -1)
        {
            lblStatusError.Visible = true;
            status = false;
        }
        else
        {
            lblStatusError.Visible = false;
        }
        if (DropDownList1.SelectedValue == "-1")
        {
            lblUserTypeError.Visible = true;
            status = false;
        }
        else
        {
            lblUserTypeError.Visible = false;
        }
        return status;
    }

    protected bool checkDuplicate()
    {
        bool status = true;
        myDataSet.AccountsDataTable accTable = accAdapter.CheckUserNameDuplicate(txtUser.Text.Trim());
        if (accTable.Rows.Count != 0)
        {
            lblUserDuplicateError.Visible = true;
            status = false;
        }
        else
        {
            lblUserDuplicateError.Visible = false;
        }
        return status;
    }

    protected void cmdSearchEnable_Click(object sender, EventArgs e)
    {
        display();
        PanelSearchAddInvi1.Visible = false;
        PanelSearchEditInvi1.Visible = false;
        PanelEditInvi1.Visible = true;
        txtId.Enabled = true;
        txtId.BackColor = System.Drawing.Color.White;
        cmdSearch.Text = "Search";
        cmdClear.Visible = true;
        clear();
        if (cmdSearchEnable.Text == "Enable Search Area")
        {
            Panel1.Visible = true;
            cmdSearchEnable.Text = "Disable Search Area";
            cmdAddEnable.Text = "Enable Add Area";
        }
        else
        {
            Panel1.Visible = false;
            cmdSearchEnable.Text = "Enable Search Area";
        }
    }

    protected void cmdAddEnable_Click(object sender, EventArgs e)
    {
        display();
        PanelSearchEditInvi1.Visible = true;
        PanelEditInvi1.Visible = true;
        PanelSearchAddInvi1.Visible = false;
        txtId.Enabled = false;
        txtId.BackColor = System.Drawing.Color.WhiteSmoke;
        cmdSearch.Text = "Add new";
        cmdClear.Visible = true;
        clear();
        if (cmdAddEnable.Text == "Enable Add Area")
        {
            Panel1.Visible = true;
            cmdAddEnable.Text = "Disable Add Area";
            cmdSearchEnable.Text = "Enable Search Area";
        }
        else
        {
            Panel1.Visible = false;
            cmdAddEnable.Text = "Enable Add Area";
        }
    }

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        if (cmdSearch.Text == "Search")
        {
            try
            {
                Panel1.Visible = true;
                string strSQL = "SELECT id, userName, pass, fullName, rollNo, admissionDate, is_active, userType, CONVERT(varchar(10), admissionDate, 103) AS date, CASE is_active WHEN 1 THEN 'Active' ELSE 'Inactive' END AS status, CASE userType WHEN 1 THEN 'Admin' WHEN 2 THEN 'Faculty' ELSE 'Member' END AS uType FROM Accounts where";
                strSQL += " userName like N'%" + txtUser.Text.Trim() + "%'";
                strSQL += " and fullName like N'%" + txtFullname.Text.Trim() + "%'";
                strSQL += " and rollNo like N'%" + txtRollNo.Text.Trim() + "%'";
                if (RadDatePicker1.SelectedDate != null)
                {
                    strSQL += " and CONVERT(varchar(10), admissionDate, 103) like '" + string.Format("{0:dd/MM/yyyy}", RadDatePicker1.SelectedDate.Value) + "'";
                }
                if (RadioButtonList1.SelectedIndex != -1)
                {
                    strSQL += " and is_active = '" + RadioButtonList1.SelectedValue.ToString() + "'";
                }
                if (DropDownList1.SelectedValue != "-1")
                {
                    strSQL += " and userType = " + DropDownList1.SelectedValue.ToString();
                }
                else
                {
                    strSQL += " and userType > 0 ";
                }
                if (txtId.Text.Trim() != "")
                {
                    strSQL += " and id = " + txtId.Text.Trim();
                }
                string strConnection = ConfigurationManager.ConnectionStrings["EnvironmentSurveyConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(strConnection);
                SqlCommand sqlCmd = new SqlCommand(strSQL, conn);
                SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                if (dt.Rows.Count != 0)
                {
                    if (dt.Rows.Count == 1)
                    {
                        lblResult.Text = "There is 1 result";
                    }
                    else
                    {
                        lblResult.Text = "There are " + dt.Rows.Count + " results";
                    }
                }
                else
                {
                    lblResult.Text = "0 result... Please search again!";
                }
            }
            catch
            {
                RegisterStartupScript("as", "<script>alert('Input wrong format!')</script>");
            }
        }
        else if (cmdSearch.Text == "Add new")
        {
            if (checkEmpty())
            {
                if (checkDuplicate())
                {
                    accAdapter.InsertQuery(txtUser.Text.Trim(), txtPass.Text.Trim(), txtFullname.Text.Trim(), txtRollNo.Text.Trim(), RadDatePicker1.SelectedDate.Value, bool.Parse(RadioButtonList1.SelectedValue.ToString()), int.Parse(DropDownList1.SelectedValue.ToString()));
                    display();
                }
            }
        }
        else
        {
            if (checkEmpty())
            {
                if (int.Parse(Session["id"].ToString()) == int.Parse(txtId.Text.Trim()) && int.Parse(DropDownList1.SelectedValue.ToString())!= 1)
                {
                    RegisterStartupScript("as", "<script>alert('Cannot update type when logging in!')</script>");
                    DropDownList1.SelectedValue = "1";
                }
                else
                {
                    accAdapter.UpdateQuery(txtFullname.Text.Trim(), txtRollNo.Text.Trim(), RadDatePicker1.SelectedDate.Value, bool.Parse(RadioButtonList1.SelectedValue.ToString()), int.Parse(DropDownList1.SelectedValue.ToString()), int.Parse(txtId.Text.Trim()));
                    display();
                }
            }
        }
    }

    protected void cmdClear_Click(object sender, EventArgs e)
    {
        clear();
        display();
        Panel1.Visible = true;
    }

    protected void cmdHide_Click(object sender, EventArgs e)
    {
        display();
        Panel1.Visible = false;
        cmdSearchEnable.Text = "Enable Search Area";
        cmdAddEnable.Text = "Enable Add Area";
        lblResult.Text = null;
    }

    protected void getDataById(int id)
    {
        myDataSet.AccountsDataTable accTable = accAdapter.GetDataById(id);
        txtId.Text = accTable[0]["id"].ToString();
        txtUser.Text = accTable[0]["userName"].ToString();
        txtFullname.Text = accTable[0]["fullName"].ToString();
        txtRollNo.Text = accTable[0]["rollNo"].ToString();
        RadDatePicker1.SelectedDate = DateTime.Parse(accTable.Rows[0]["admissionDate"].ToString());
        RadioButtonList1.SelectedValue = accTable[0]["is_active"].ToString();
        DropDownList1.SelectedValue = accTable[0]["userType"].ToString();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        Panel1.Visible = true;
        PanelSearchAddInvi1.Visible = true;
        PanelEditInvi1.Visible = false;
        PanelSearchEditInvi1.Visible = false;
        txtId.Enabled = false;
        txtId.BackColor = System.Drawing.Color.WhiteSmoke;
        cmdSearch.Text = "Save";
        cmdClear.Visible = false;
        cmdSearchEnable.Text = "Enable Search Area";
        cmdAddEnable.Text = "Enable Add Area";
        clear();
        getDataById(id);
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            if (int.Parse(Session["id"].ToString()) == id)
            {
                RegisterStartupScript("as", "<script>alert('Cannot delete yourself!')</script>");
            }
            else
            {
                accAdapter.DeleteQuery(id);
                clear();
                display();
            }
        }
        catch
        {
            RegisterStartupScript("as", "<script>alert('Cannot delete!')</script>");
        }
    }
    protected void resetPassButton_Click(object sender, EventArgs e)
    {
        accAdapter.ResetPassword(int.Parse(txtId.Text.ToString()));
        RegisterStartupScript("as","<script>alert('Password default is: 123');</script>");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        display();
    }
}
